/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 * Licensed under the Apache License, Version 2.0(the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ETH_GETHMAC_H
#define ETH_GETHMAC_H

#include <stdio.h>
#include <stdbool.h>

#include "Platform_Types.h"
#include "Eth_GeneralTypes.h"

#define ETH_FRAMEHEADER_LENGTH (14U)
#define ETH_FRAMECHKSUM_LENGTH (4U)
#define ETH_MACADDRESS_LEN (6u)
#define ETH_FRAME_TYPE_POS_MSB (12U)
#define ETH_FRAME_TYPE_POS_LSB (13U)
#define ETH_FRAME_SHIFT_8BIT (8U)
#define ETH_FRAME_BYTE_MASK (0xFFU)
#define ETH_FRAME_DESTADDR_POS (0U)
#define ETH_FRAME_PAYLOAD_POS (ETH_FRAMEHEADER_LENGTH)
#define ETH_FRAME_SRCADDR_POS (6U)
#define ETH_GETHMAC_ZERO (0U)
#define ETH_GETHMAC_ONE (1U)
#define ETH_GETHMAC_TWO (2U)
#define ETH_GETHMAC_THREE (3U)
#define ETH_GETHMAC_FOUR (4U)
#define ETH_GETHMAC_FIVE (5U)
#define ETH_GETHMAC_SHIFT_8BIT (8U)
#define ETH_GETHMAC_SHIFT_16BIT (16U)
#define ETH_GETHMAC_SHIFT_21BIT (21U)
#define ETH_GETHMAC_SHIFT_24BIT (24U)

#define ETH_TX_DESC_FD_SET (0x20000000U)
#define ETH_TX_DESC_LD_SET (0x10000000U)
#define ETH_TX_DESC_OWN_SET (0x80000000U)
#define ETH_MASK_TDES2_BUFLENGTH (0xFFFFC000U)
#define ETHC_DMA_TX_DESC_PAC_LEN_MASK (0x00007FFFU)

#define ETH_DMA_CH_STAT_RI_RESET (0x00000040U)
#define ETH_DMA_CH_STAT_RBU_ERI_AIE_RESET (0x00004880U)
#define ETH_DMA_CH_STAT_NIS_RESET (0x00008000U)

#define IFXGETH_RX_INT_MAXLOOPCNT (5u)
#define IFXGETH_HEADER_LENGTH (14U)

#define ETH_BUFFER_NOT_USED (0U)
#define ETH_BUFFER_USED (1U)

#define ETH_TX_BUFFER_NO_INITED (0U)
#define ETH_TX_BUFFER_INITED (1U)

#define ETH_NO_NEED_TXCONFIRMATION (0U)
#define ETH_NEED_TXCONFIRMATION (1U)

typedef struct {
	uint16 len_req;
	uint8 owner_status;
	uint8 tx_confirm_req;
	uint8 inited;
} eth_tx_buffer_crtl_type;

void eth_gethmac_init(void);
/* Tx Confirm */
void ethisr2_isr(void);
/* Rx */
void ethisr6_isr(void);

void eth_enable_eth_mac(void);

BufReq_ReturnType ETH_ProvideTxBuffer(const uint8 ctrl_idx, uint8 priority,
	eth_buf_idx_type *const bufidx_ptr, uint8 **const buf_ptr, uint16 *const lenbyte_ptr);
Std_ReturnType ETH_Transmit(uint8 ctrl_idx, eth_buf_idx_type buf_idx, eth_frame_type frame_type,
	boolean tx_confirmation, uint16 len_byte, const uint8 *phys_addr_ptr);
void ETH_GetPhysAddr(uint8 ctrl_idx, uint8 *phys_addr_ptr);

#endif
